package com.sec.etech.org.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.sec.etech.org.service.EtechGroupService;
import com.sec.etech.sys.util.ContextUtil;
import org.apache.ibatis.session.RowBounds;
import org.openbpm.base.api.aop.annotion.CatchErr;
import org.openbpm.base.api.query.QueryFilter;
import org.openbpm.base.api.query.QueryOP;
import org.openbpm.base.api.response.impl.ResultMsg;
import org.openbpm.base.db.model.query.DefaultPage;
import org.openbpm.base.db.model.query.DefaultQueryFilter;
import org.openbpm.org.core.constant.RelationTypeConstant;
import org.openbpm.org.core.manager.GroupManager;
import org.openbpm.org.core.manager.OrgRelationManager;
import org.openbpm.org.core.manager.UserManager;
import org.openbpm.org.core.model.Group;
import org.openbpm.org.core.model.OrgRelation;
import org.openbpm.org.rest.controller.GroupController;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/etech/group")
public class EtechGroupController extends GroupController {

    @Resource
    EtechGroupService etechGroupService;
    @Resource
    GroupManager groupManager;
    @Resource
    UserManager userManager;
    @Resource
    OrgRelationManager orgRelationMananger;


    public long getGroupMembers(String groupId, boolean isContains){

        QueryFilter queryFilter = new DefaultQueryFilter();
        RowBounds rowBounds = new RowBounds(0, 10);
        DefaultPage page = new DefaultPage(rowBounds);
        queryFilter.setPage(page);

        if (isContains) {
            List<String> groupIds = etechGroupService.getAllDprtIdsByDprtId(groupId);
            queryFilter.addFilter("tgroup.id_", groupIds, QueryOP.IN);
        }else{
            queryFilter.addFilter("tgroup.id_", groupId, QueryOP.EQUAL);
        }


        queryFilter.addFilter("relation.type_", Arrays.asList(RelationTypeConstant.GROUP_USER.getKey(), RelationTypeConstant.POST_USER.getKey()), QueryOP.IN);

        Page<OrgRelation> pageList = (Page<OrgRelation>) this.orgRelationMananger.query(queryFilter);
        System.out.println(pageList);
        return pageList.getTotal();
    }

    /**
     * 刷新组织部门调增后的路径，层级和是否底层数据
     */
    @RequestMapping("/repair")
    @CatchErr
    public ResultMsg repairGroupList() {
        etechGroupService.repair();
        return getSuccessResult();
    }

    @RequestMapping({"get"})
    @CatchErr
    public ResultMsg get(@RequestParam String id) throws Exception {
        Group group = (Group)this.groupManager.get(id);
        Map<String, Object> map = null;
        if (group != null && group.getParentId() != null ) {
            if (!group.getParentId().equals("0")){
                String parentOrgName = ((Group)this.groupManager.get(group.getParentId())).getName();
                group.setParentName(parentOrgName);

                map = BeanUtil.beanToMap(group);
                map.put("groupFullName", etechGroupService.getFullGroupName(id));
                map.put("groupOrgRelations",etechGroupService.getOrgRelationByGroup(id));
                map.put("groupMember", getGroupMembers(id, false));
                map.put("groupMemberAll", getGroupMembers(id, true));
            }else {
                map = BeanUtil.beanToMap(group);
                map.put("groupFullName", etechGroupService.getFullGroupName(id));
                map.put("groupOrgRelations",etechGroupService.getOrgRelationByGroup(id));
                map.put("groupMember", getGroupMembers(id, false));
                map.put("groupMemberAll", getGroupMembers(id, true));
            }

        }
        return this.getSuccessResult(map);
    }

    /**
     * 获取所有部门ID（包含本身以及所有子部门，包含多层子部门）
     */
    @RequestMapping("/getAllDprtIdsByDprtId")
    @CatchErr
    public ResultMsg getAllDprtIdsByDprtId(@RequestParam("dprtid") String dprtid) {
        return getSuccessResult(etechGroupService.getAllDprtIdsByDprtId(dprtid));
    }


    @RequestMapping("/getCurrentMasterGroupName")
    public ResultMsg getCurrentMasterGroupName() {
        return getSuccessResult(ContextUtil.getCurrentMasterGroupName());
    }


}
